<?php

namespace Action;
use Library\Auth\Session;

class User extends \Library\Action\PortalAction {

	private $_user = null;
	private $_searchBar = null;
	private $_contactsBox = null;
	private $_tabPane = null;

	public function execute() {
		parent::execute();
		
		if( !$this->hasParam('id') && !Session::getSession()->isAuthenticated() ) {
			$this->redirect( './' );
		}

		$this->setTitle( $this->getUser()->getFullName() );
		$this->setIcon( $this->getUser()->getShortcutIcon() );
	}
	
	public function getUserId() {
		return $this->hasParam( 'id' ) ?
			$this->getParam( 'id' ) :
				Session::getSession()->getUser()->getUserId();
	}
	
	public function getUser() {
		if( $this->_user === null ) {
			try {
				$this->_user = \Bean\User::get( $this->getUserId() );
			}
			catch( \Exception $e ) {
				header('HTTP/1.0 404 Not Found');
				throw new \Exception( 'User not found : ' . $this->getUserId() );
			}
		}
		return $this->_user;
	}
	
	public function getTabPane() {
		if( $this->_tabPane === null ) {
			$this->_tabPane = new \Library\Widget\TabPane();
			if( $this->getUser()->isMe() ) {
				$this->_tabPane->getTab( 'feed' )
					->setTitle( 'Feed' )
					->setAction( new User\Feed( array( 'id' => $this->getUserId() ) ) );
			}
			$this->_tabPane->getTab( 'wall' )
				->setTitle( 'Wall' )
				->setAction( new User\Wall( array( 'id' => $this->getUserId() )  ) );
			$this->_tabPane->getTab( 'rss' )
				->setTitle( 'RSS' )
				->setAction( new User\Rss( array( 'id' => $this->getUserId() ) ) );
		}
		return $this->_tabPane;
	}
	
	public function getSearchBar() {
		if( $this->_searchBar === null ) {
			$this->_searchBar = new \Library\Widget\SearchBar( _u() );
		}
		return $this->_searchBar;
	}
	
	public function getOptions() {
		if( $this->_options === null ) {
			$this->_options = $options = new \Library\Widget\Options();
			if( $this->getUser()->isContactable() ) {
				$contact = $options->getGroup( 'contact' )
					->setTitle( 'Contact' )
					->setAttribute( 'user_id', $this->getUser()->getUserId() );
				
				if( $this->getUser()->isContact() ) {
					$contact
						->getOption( 'remove' )
						->setAction( 'contact_remove' )
						->setTitle( 'Remove from contacts' );
				}
				else if( $this->getUser()->isAwaitingConfirmation() ) {
					$contact
						->getOption( 'answer' )
						->setAction( 'contact_answer' )
						->setAttribute( 'request_id', $this->getUser()->getRequest()->getRequestId() )
						->setTitle( 'Answer to request' );
				}
				else if( !_u()->isAwaitingConfirmation( $this->getUser() ) ) {
					$contact
						->getOption( 'add' )
						->setAction( 'contact_add' )
						->setTitle( 'Add to contacts' );
				}
			}
		}
		return $this->_options;
	}
	
	public function getContactsBox() {
		if( $this->_contactsBox === null ) {
			$this->_contactsBox = new \Library\Widget\Box\Contacts( $this->getUser() );
		}
		return $this->_contactsBox;
	}

}
